package com.css.zfzx.sjcj.modules.statistics.hdcDResultmaptable.repository.impl;

import com.css.zfzx.sjcj.common.vo.DivisionVo;
import com.css.zfzx.sjcj.modules.statistics.hdcDResultmaptable.repository.HdcDResultmaptableRepository;
import com.css.zfzx.sjcj.modules.statistics.hdcDResultmaptable.repository.entity.HddcDResultmaptableEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: pc-statistics
 * @Author: lhl
 * @CreateDate: 2021/5/13 11:17
 */
@Repository
public class HdcDResultmaptableRepositoryImpl implements HdcDResultmaptableRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(HdcDResultmaptableRepositoryImpl.class);

    @Override
    public List<HddcDResultmaptableEntity> findCreateTime(LocalDateTime createTime,int identify) {
        StringBuilder sql = new StringBuilder("select * from hddc_d_resultmaptable");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (createTime!=null) {
            if (createTime.isEqual(LocalDateTime.parse("2021-08-02 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))){
                whereSql.append(" and create_time < :createTime");
            }else {
                whereSql.append(" and create_time > :createTime");
            }
        }else {
            whereSql.append(" and create_time is not null");
        }
        if (identify==1){
            whereSql.append(" and extends20 ='1'");//1：5万活动断层分布图
        }else if (identify==2){
            whereSql.append(" and extends20 ='4'");//1：5万活动断层分布图说明书
        }
        else if (identify==3){
            whereSql.append(" and extends20 ='5'");//1：5万活动断层分布图成果报告
        }
        else if (identify==4){
            whereSql.append(" and extends20 ='2'");//1：25万地震构造图
        }
        else if (identify==5){
            whereSql.append(" and extends20 ='6'");//1：25万地震构造图说明书
        }
        else if (identify==6){
            whereSql.append(" and extends20 ='7'");//1：25万地震构造图成果报告
        }
        // 删除标识为1：有效
        whereSql.append(" and is_valid = 1");
        whereSql.append(" and update_time is null");
        whereSql.append(" order by create_time desc");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HddcDResultmaptableEntity.class);

        // 时间
        if (createTime!=null) {
            //query.setParameter("createTime", createTime.toLocalDate()+" "+createTime.toLocalTime());
            query.setParameter("createTime", createTime);
        }
        List<HddcDResultmaptableEntity> list = query.getResultList();
        return list;
    }

    @Override
    public List<HddcDResultmaptableEntity> findUpdateTime(LocalDateTime updateTime,int identify) {
        StringBuilder sql = new StringBuilder("select * from hddc_d_resultmaptable");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (updateTime!=null) {
            if (updateTime.isEqual(LocalDateTime.parse("2021-08-02 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))){
                whereSql.append(" and update_time < :updateTime");
                whereSql.append(" and is_valid = 1");
            }else {
                whereSql.append(" and update_time > :updateTime");
            }
        }else {
            whereSql.append(" and update_time is not null");
            whereSql.append(" and is_valid = 1");
        }
        if (identify==1){
            whereSql.append(" and extends20 ='1'");//1：5万活动断层分布图
        }else if (identify==2){
            whereSql.append(" and extends20 ='4'");//1：5万活动断层分布图说明书
        }
        else if (identify==3){
            whereSql.append(" and extends20 ='5'");//1：5万活动断层分布图成果报告
        }
        else if (identify==4){
            whereSql.append(" and extends20 ='2'");//1：25万地震构造图
        }
        else if (identify==5){
            whereSql.append(" and extends20 ='6'");//1：25万地震构造图说明书
        }
        else if (identify==6){
            whereSql.append(" and extends20 ='7'");//1：25万地震构造图成果报告
        }
        whereSql.append(" order by update_time desc");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HddcDResultmaptableEntity.class);
        // 时间
        if (updateTime!=null) {
            query.setParameter("updateTime", updateTime);
        }
        List<HddcDResultmaptableEntity> list = query.getResultList();
        return list;
    }

    @Override
    public List<DivisionVo> count(int identify1, int identify) {
        StringBuilder sql = new StringBuilder("select province,city,area from hddc_d_resultmaptable");
        StringBuilder whereSql = new StringBuilder(" where is_valid = 1 ");
        if (identify1==1){
            whereSql.append(" and is_report_pg = '1'");
        }else if (identify1==2){
            whereSql.append(" and (is_report_pg = '0' or is_report_pg is null)");
        }
        if (identify==1){
            whereSql.append(" and extends20 ='1'");//1：5万活动断层分布图
        }else if (identify==2){
            whereSql.append(" and extends20 ='4'");//1：5万活动断层分布图说明书
        }
        else if (identify==3){
            whereSql.append(" and extends20 ='5'");//1：5万活动断层分布图成果报告
        }
        else if (identify==4){
            whereSql.append(" and extends20 ='2'");//1：25万地震构造图
        }
        else if (identify==5){
            whereSql.append(" and extends20 ='6'");//1：25万地震构造图说明书
        }
        else if (identify==6){
            whereSql.append(" and extends20 ='7'");//1：25万地震构造图成果报告
        }
        whereSql.append(" group by province,city,area");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString());

        List<Object[]> resultList = query.getResultList();
        List<DivisionVo> voList=new ArrayList<>();
        for (Object[] objects:resultList){
            DivisionVo vo=new DivisionVo();
            vo.setProvinceName(String.valueOf(objects[0]));
            vo.setCityName(String.valueOf(objects[1]));
            vo.setAreaName(String.valueOf(objects[2]));
            voList.add(vo);
        }
        return voList;
    }
}
